package com.itany.boot.nmms.service.impl;

import com.itany.boot.nmms.constant.DistinctConstant;
import com.itany.boot.nmms.constant.StatusConstant;
import com.itany.boot.nmms.dao.DeptMapper;
import com.itany.boot.nmms.dao.SequenceMapper;
import com.itany.boot.nmms.entity.*;
import com.itany.boot.nmms.exception.DeptExistException;
import com.itany.boot.nmms.exception.RequestParamsException;
import com.itany.boot.nmms.service.DeptService;
import com.itany.boot.nmms.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private SequenceMapper sequenceMapper;

    @Override
    public List<Dept> findAll() {
        List<Dept> depts = deptMapper.findAll();
        return depts.isEmpty() ? null : depts;
    }

    @Override
    public void addDept(String fatherId, String name, String duty, HttpSession session) throws RequestParamsException, DeptExistException {
        if (ParamsUtil.isNull(name) || ParamsUtil.isNull(duty)) {
            throw new RequestParamsException("请求参数异常");
        }

        DeptExample deptExample = new DeptExample();
        deptExample.or()
                .andDeptNameEqualTo(name);
        List<Dept> depts = deptMapper.selectByExample(deptExample);
        if (!depts.isEmpty()) {
            throw new DeptExistException("该部门已存在");
        }

        Dept dept = new Dept();
        dept.setDeptName(name);
        if (!ParamsUtil.isNull(fatherId)) {
            dept.setFatherDeptId(Integer.valueOf(fatherId));
        }
        dept.setRemark(duty);
        dept.setCreateDate(new Date());
        Staff staff = (Staff) session.getAttribute("staff");
        dept.setCreateStaffId(staff.getStaffId());
        dept.setIsValid(Integer.valueOf(StatusConstant.ENABLE));

        SequenceExample example = new SequenceExample();
        example.or()
                .andNameEqualTo(DistinctConstant.DEPARTMENT_PREFIX);
        List<Sequence> sequences = sequenceMapper.selectByExample(example);
        if (sequences.isEmpty()) {
            Sequence sequence = new Sequence();
            sequence.setName(DistinctConstant.DEPARTMENT_PREFIX);
            sequence.setValue(DistinctConstant.DEPT_SEQUENCE_INIT_VALUE);
            sequenceMapper.insert(sequence);
            dept.setDeptNo(DistinctConstant.DEPARTMENT_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequence.getValue());

        } else {
            Sequence sequence = sequences.get(0);
            if (sequence.getValue().equals(DistinctConstant.DEPT_SEQUENCE_MAX_VALUE)) {
                sequence.setValue(DistinctConstant.DEPT_SEQUENCE_INIT_VALUE);
            } else {
                sequence.setValue(String.format("%05d", Integer.parseInt(sequence.getValue()) + 1));
            }
            sequenceMapper.updateByPrimaryKeySelective(sequence);
            dept.setDeptNo(DistinctConstant.DEPARTMENT_PREFIX + new SimpleDateFormat("yyyyMMdd").format(new Date()) + sequence.getValue());
        }


        deptMapper.insertSelective(dept);


    }

    @Override
    public Dept findById(String id) throws RequestParamsException {
        if (ParamsUtil.isNull(id)) {
            throw new RequestParamsException("请求参数异常");
//            DeptExample example = new DeptExample();
//            example.or()
//                    .andDeptIdIsNull();
//            List<Dept> depts = deptMapper.selectByExample(example);
//            return depts.isEmpty()? null : depts.get(0);
        }
        Dept dept = deptMapper.selectByPrimaryKey(Integer.valueOf(id));

        return dept;

    }

    @Override
    public void modifyStatus(String id) throws RequestParamsException {
        if (ParamsUtil.isNull(id)) {
            throw new RequestParamsException("请求参数异常");
        }

        Dept dept = deptMapper.selectByPrimaryKey(Integer.valueOf(id));
        Dept dept1 = new Dept();
        dept1.setDeptId(Integer.valueOf(id));
        if (dept.getIsValid().equals(Integer.parseInt(StatusConstant.ENABLE))) {
            dept1.setIsValid(Integer.valueOf(StatusConstant.DISABLE));
        } else if (dept.getIsValid().equals(Integer.parseInt(StatusConstant.DISABLE))) {
            dept1.setIsValid(StatusConstant.STAFF_IS_VALID_ENABLE);
        }
        deptMapper.updateByPrimaryKeySelective(dept1);


    }

    @Override
    public void modifyDept(String id, String name, String duty) throws RequestParamsException, DeptExistException {
        if (ParamsUtil.isNull(id) || ParamsUtil.isNull(name) || ParamsUtil.isNull(duty)) {
            throw new RequestParamsException("请求参数异常");
        }

        DeptExample deptExample = new DeptExample();
        deptExample.or()
                .andDeptNameEqualTo(name)
                .andDeptIdNotEqualTo(Integer.valueOf(id));
        List<Dept> depts = deptMapper.selectByExample(deptExample);
        if (!depts.isEmpty()) {
            throw new DeptExistException("该部门已存在");
        }

        Dept dept = new Dept();
        dept.setDeptId(Integer.valueOf(id));
        dept.setDeptName(name);
        dept.setRemark(duty);
        deptMapper.updateByPrimaryKeySelective(dept);

    }
}
